<?php
namespace app\api\controller;

header('Access-Control-Allow-Origin:*');
header("Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept,USER_ID,TOKEN");
header("Access-Control-Allow-Methods:HEAD, GET, POST, DELETE, PUT, OPTIONS");

use think\Controller;
use think\Db;

class App extends Controller
{

	/* API入口文件 */
	public function index()
	{


		$params = request()->param(); // 获取参数
		$action = request()->route('action'); // 请求接口方法
		$method_type = request()->method(); // 请求类型
		try {
            // 验证接口请求方身份
            $check_data = check_merchant($params, $action);
            if ($check_data['code'] != 200) {
                return json($check_data);
            }

            $param_data = isset($params['data']) ? $params['data'] : [];
            // 调用API
			$return = invoke_api($action, $param_data, $method_type);

            if ($return['code'] == 200) {
                // 获取数据成功， 添加接口请求记录
                $add_interface_request_log = add_interface_request_log($param_data, $action);
                if ($add_interface_request_log['code'] != 200) {
                    return json($add_interface_request_log);
                }

                return json($return);
            } else {
                throw new \Exception($return['msg']);
            }
		} catch (\Exception $e) {
			return json([
                'data' => [], 'code' => 0,'msg' => $e->getMessage(),'interface' => $action
			]);
		}
	}
}
